package com.lxw.nonghe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxw.nonghe.annotation.RequiresPermission;
import com.lxw.nonghe.common.Result;
import com.lxw.nonghe.entity.Family;
import com.lxw.nonghe.service.FamilyService;
import com.lxw.nonghe.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 家庭档案控制器
 */
@RestController
@RequestMapping("/family")
@RequiresPermission("family:manage")
public class FamilyController {

    @Autowired
    private FamilyService familyService;

    @Autowired
    private LogUtils logUtils;

    @PostMapping("/create")
    public Result<Boolean> addFamily(@RequestBody Family family) {
        try {
            // 设置创建时间
            family.setCreateTime(new Date());

            boolean result = familyService.save(family);
            if (result) {
                logUtils.recordLog("家庭档案管理", "新增家庭档案", "成功",
                        "户主:" + family.getHouseholder() + ", 家庭ID:" + family.getFamilyId());
                return Result.success("家庭档案新增成功", true);
            } else {
                logUtils.recordLog("家庭档案管理", "新增家庭档案", "失败", "保存数据库失败");
                return Result.error("家庭档案新增失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("家庭档案管理", "新增家庭档案", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/delete/{id}")
    public Result<Boolean> deleteFamily(@PathVariable Integer id) {
        try {
            Family family = familyService.getById(id);
            boolean result = familyService.removeById(id);

            if (result) {
                logUtils.recordLog("家庭档案管理", "删除家庭档案", "成功",
                        "家庭ID:" + id + ", 户主:" + (family != null ? family.getHouseholder() : "未知"));
                return Result.success("家庭档案删除成功", true);
            } else {
                logUtils.recordLog("家庭档案管理", "删除家庭档案", "失败", "家庭ID:" + id + "不存在");
                return Result.error("家庭档案删除失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("家庭档案管理", "删除家庭档案", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result<Boolean> updateFamily(@RequestBody Family family) {
        try {
            boolean result = familyService.updateById(family);
            if (result) {
                logUtils.recordLog("家庭档案管理", "更新家庭档案", "成功",
                        "家庭ID:" + family.getFamilyId());
                return Result.success("家庭档案更新成功", true);
            } else {
                logUtils.recordLog("家庭档案管理", "更新家庭档案", "失败",
                        "家庭ID:" + family.getFamilyId() + "不存在");
                return Result.error("家庭档案更新失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("家庭档案管理", "更新家庭档案", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @GetMapping("/get/{id}")
    public Result<Family> getFamilyById(@PathVariable Integer id) {
        try {
            Family family = familyService.getById(id);
            if (family != null) {
                /*logUtils.recordLog("家庭档案管理", "查询家庭详情", "成功", "家庭ID:" + id);*/
                return Result.success("家庭档案查询成功", family);
            } else {
                logUtils.recordLog("家庭档案管理", "查询家庭详情", "失败", "家庭ID:" + id + "不存在");
                return Result.error("未找到该家庭档案");
            }
        } catch (Exception e) {
            logUtils.recordLog("家庭档案管理", "查询家庭详情", "失败", e.getMessage());
            return Result.error("查询异常: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result<Page<Family>> getFamilyPage(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String householder,
            @RequestParam(required = false) String address,
            @RequestParam(required = false) String registrar,
            @RequestParam(required = false) Integer population,
            @RequestParam(required = false) Integer agriculturalPopulation) {

            Page<Family> page = familyService.getFamilyPage(current, size,
                householder, address, registrar, population, agriculturalPopulation);
            return Result.success("查询成功", page);
        }

    @PostMapping("/query")
    public Result<Page<Family>> queryFamilies(
            @RequestBody Family queryParam,
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Page<Family> familyPage = familyService.queryFamily(queryParam, current, size);
            /*logUtils.recordLog("家庭档案管理", "条件查询家庭", "成功",
                    "条件:" + queryParam.toString() + ", 页码:" + current + ", 大小:" + size);*/
            return Result.success("家庭档案条件查询成功", familyPage);
        } catch (Exception e) {
            logUtils.recordLog("家庭档案管理", "条件查询家庭", "失败", e.getMessage());
            return Result.error("条件查询异常: " + e.getMessage());
        }
    }
}
